package com.foo.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.foo.app.R;

import java.text.DateFormat;
import java.util.Date;

public class ProductItemAdapter extends BaseAdapter {

    // 声明
    private Context context;
    private LayoutInflater layoutInflater;

    public ProductItemAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * 静态类: 封装控件
     */
    static class ViewHolder {
        private ImageView listIv;
        private TextView listTv1, listTv2, listTv3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 获取控件
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.dish_list_view, null);
            viewHolder = new ViewHolder();
            viewHolder.listIv = convertView.findViewById(R.id.list_iv);
            viewHolder.listTv1 = convertView.findViewById(R.id.list_tv1);
            viewHolder.listTv2 = convertView.findViewById(R.id.list_tv2);
            viewHolder.listTv3 = convertView.findViewById(R.id.list_tv3);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 赋值控件
        viewHolder.listTv1.setText("这是一个标题");
        viewHolder.listTv2.setText(DateFormat.getDateTimeInstance().format(new Date()));
        viewHolder.listTv3.setText("这是一个描述");
        // 复制网络照片
//        Glide.with(context).load("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=295289102,3688591987&fm=26&gp=0.jpg").into(viewHolder.listIv);
        return convertView;
    }
}
